/*
 * @Author: 葛文杰
 * @Date: 2021/1/29 15:46
 * @Description: egg proxy 数据源
 */
const { IS_NOT_DELETE, SQL_DATA_FORMAT, BASE_WHERE_CONDITION_SQL } = require('../util/common');

const TABLE_NAME = 'data_source_config'; // 表名

const TABLE_FIELD = `id,dataSourceName,dataSourceTableName,dataSourceInterfaceUrl,dataSourcePullType,dataSourcePullRate,dataSourcePullDay,dataSourcePullTime,dataSourceStatus`;

module.exports = {

    /**
     * 获取数据源列表
     */
    async getDataSourceConfigList(conn, { dataSourceName, dataSourcePullType, dataSourceStatus, pageNo, pageSize }) {
        pageNo = parseInt(pageNo);
        pageSize = parseInt(pageSize);
        let start = (pageNo - 1) * pageSize;
        let selectSql =
          `select ${TABLE_FIELD},
          createUser as createUserName,
          DATE_FORMAT(createAt,'${SQL_DATA_FORMAT}') as createAt 
          from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL}`;
        let params = [];
        conditionSql = `${conditionSql} and isDelete = ?`;
        params.push(IS_NOT_DELETE);
        if (dataSourceName) {
            conditionSql = `${conditionSql} and dataSourceName like concat('%',?,'%')`;
            params.push(dataSourceName);
        }
        if (dataSourcePullType) {
            conditionSql = `${conditionSql} and dataSourcePullType = ?`;
            params.push(dataSourcePullType);
        }
        if (dataSourceStatus) {
            conditionSql = `${conditionSql} and dataSourceStatus = ?`;
            params.push(dataSourceStatus);
        }
        let sql = `${selectSql} ${conditionSql} limit ?,?`;
        params = [ ...params, start, pageSize ];
        let dataSourceConfigList = await conn.query(sql, params);
        return dataSourceConfigList;
    },

    /**
     * 获取数据源总数
     */
    async getDataSourceConfigTotal(conn, { dataSourceName, dataSourcePullType, dataSourceStatus }) {
        let selectSql = `select count(1) as count from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL} and isDelete = ?`;
        let params = [ IS_NOT_DELETE ];
        if (dataSourceName) {
            conditionSql = `${conditionSql} and dataSourceName like concat('%',?,'%')`;
            params.push(dataSourceName);
        }
        if (dataSourcePullType) {
            conditionSql = `${conditionSql} and dataSourcePullType = ?`;
            params.push(dataSourcePullType);
        }
        if (dataSourceStatus) {
            conditionSql = `${conditionSql} and dataSourceStatus = ?`;
            params.push(dataSourceStatus);
        }
        let sql = `${selectSql} ${conditionSql}`;
        let [ total ] = await conn.query(sql, params);
        let { count } = total;
        return count;
    },

    /**
     * 通过Id获取数据源信息
     * @param conn
     * @param condition
     * @return {Promise<any|Credential>}
     */
    async getDataSourceConfigById(conn, condition) {
        const dataSourceConfig = await conn.get(TABLE_NAME, condition);
        return dataSourceConfig;
    },

    /**
     * 存储数据源
     * @param conn
     * @param dataSourceConfig
     * @return {Promise<void>}
     */
    async insertDataSourceConfigInfo(conn, dataSourceConfig) {
        let result = await conn.insert(TABLE_NAME, dataSourceConfig);  // 第一步操作
        return result;
    },

    /**
     * 更新数据源
     * @param conn
     * @param dataSourceConfig
     * @return {Promise<void>}
     */
    async updateDataSourceConfig(conn, dataSourceConfig) {
        let result = await conn.update(TABLE_NAME, dataSourceConfig);  // 第一步操作
        return result;
    }
};
